package com.tommy.jiami.adapter;

import android.view.*;
import android.widget.*;
import android.content.*;
import java.util.*;
import com.tommy.jiami.utils.*;
import com.tommy.jiami.*;

public class MediaAdapter extends BaseAdapter
{
	private Context mContext;
	private int mMediaType;
	private List<Media> mMedias;
	private int width=-1;
	
	public MediaAdapter(Context context,int mediaType,List<Media> medias){
		mContext=context;
		mMediaType=mediaType;
		mMedias=medias;
	}

	public List<Media> getMedias()
	{
		return mMedias;
	}

	public void setMediaWidth(int width)
	{
		this.width=width;
	}
	
	public void changeMedias(List<Media> medias)
	{
		mMedias=medias;
		notifyDataSetChanged();
	}
		
	@Override
	public int getCount()
	{
		return mMedias.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mMedias.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Media media=(Media)getItem(position);
		ViewHolder holder;
		if(convertView==null){
			convertView=LayoutInflater.from(mContext).inflate(R.layout.item_media,null);
			holder=new ViewHolder();
			holder.image=(ImageView)convertView.findViewById(R.id.media_item_image);
			if(width!=-1)holder.image.setLayoutParams(new LinearLayout.LayoutParams(width,width));
			holder.path=(TextView)convertView.findViewById(R.id.media_item_path);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		holder.image.setImageBitmap(media.getThumbnail());
		holder.path.setText(media.getName());
		return convertView;
	}

	@Override
	public boolean hasStableIds()
	{
		return true;
	}	
	
	class ViewHolder{
		ImageView image;
		TextView path;
	}
	
}
